use slint::SharedString;

pub fn string_to_shared_string(string_list: &Vec<String>) -> Vec<SharedString> {
    let mut shared_string_list = Vec::new();
    for item in string_list {
        shared_string_list.push(item.to_string().into());
    }
    shared_string_list
}

pub fn parse_number_str(num_str: &str) -> Option<i64> {
    //print!("parse_number_str {}  ", num_str);

    let num_str = num_str.trim().replace(" ", "").replace("_", "");
    let negative = num_str.starts_with('-');
    let mut num_str = num_str.trim_start_matches("-").to_lowercase(); //remove negative
    let mut ratio = 1;

    if num_str.contains("k") {
        ratio = 1024;
        num_str = if let Some(s) = num_str.split("k").next() {
            s.to_string()
        } else {
            "".to_string()
        };
    } else if num_str.contains("m") {
        ratio = 1024 * 1024;
        num_str = if let Some(s) = num_str.split("m").next() {
            s.to_string()
        } else {
            "".to_string()
        };
    }

    let mut result = None;
    if num_str.starts_with("0x") {
        match i64::from_str_radix(&num_str.trim_start_matches("0x"), 16) {
            Ok(mut value) => {
                if negative {
                    value = -value;
                }
                result = Some(value * ratio)
            }
            Err(e) => {
                println!("i64::from_str_radix err: {:?}", e);
            }
        }
    } else {
        if let Ok(mut value) = num_str.parse::<i64>() {
            if negative {
                value = -value;
            }
            result = Some(value * ratio)
        }
    }
    //println!("result: {:?}", result);
    return result;
}
